// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify infoTree
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package app.utils
{
    import copyengine.infoTree.meta.CESAnimationEachFrameInfoMeta;
    import copyengine.infoTree.meta.CESDisplayObjectMeta;
    import copyengine.infoTree.meta.CESRectangleMeta;
    import copyengine.infoTree.meta.CESTextFieldMeta;

    import flash.display.DisplayObject;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import flash.text.TextFormat;

    public final class MetaInfoUtils
    {
        public function MetaInfoUtils()
        {
        }

        /**
         * 取得Target当前帧的变化矩阵(Alpha和Transform信息)
         */
        public static function getTargetFrameMatrix(_traget:DisplayObject, _startIndex:int, _endIndex:int):CESAnimationEachFrameInfoMeta
        {
            var infoMeta:CESAnimationEachFrameInfoMeta = new CESAnimationEachFrameInfoMeta();
            infoMeta.setMatrix(_traget);
            infoMeta.alpha = _traget.alpha;
            infoMeta.startIndex = _startIndex;
            infoMeta.endIndex = _endIndex;
            return infoMeta;
        }

        /**
         * 当DisplayObject仅有一帧时候,使用该函数,将相关信息写入Meta中
         * 注意!! 会往frameTransformMatrixArray[0]中写入相关的信息
         */
        public static function fillDisplayObjectMetaBasicInfo(_target:DisplayObject, _meta:CESDisplayObjectMeta):void
        {
            var infoMeta:CESAnimationEachFrameInfoMeta = new CESAnimationEachFrameInfoMeta();
            infoMeta.setMatrix(_target);
            infoMeta.alpha = _target.alpha;

            _meta.name = _target.name;
            _meta.alpha = _target.alpha;
            _meta.frameTransformMatrixArray.push(infoMeta);
            //====Container应该没有Width和Height属性？
            _meta.width = _target.width;
            _meta.height = _target.height;
        }

        public static function fillScale3Or9Info(_target:DisplayObject, _meta:CESDisplayObjectMeta):void
        {
            var symbolRe:Rectangle = _target.getBounds(_target.parent);

            var infoMeta:CESAnimationEachFrameInfoMeta = new CESAnimationEachFrameInfoMeta();
            var matrix:Matrix = new Matrix();
            matrix.translate(symbolRe.left, symbolRe.top);

            infoMeta.setMatrix(matrix);
            _meta.frameTransformMatrixArray.push(infoMeta);

            _meta.name = _target.name;
            _meta.alpha = _target.alpha;
            _meta.width = _target.width;
            _meta.height = _target.height;
        }


        public static function fillTextFieldMetaInfo(_target:TextField, _textFieldMeta:CESTextFieldMeta):void
        {
            var textFormat:TextFormat = _target.defaultTextFormat;
            fillDisplayObjectMetaBasicInfo(_target, _textFieldMeta);

            _textFieldMeta.align = textFormat.align;
            _textFieldMeta.autoSize = _target.autoSize;
            _textFieldMeta.displayAsPassword = _target.displayAsPassword;
            _textFieldMeta.font = textFormat.font;
            _textFieldMeta.size = textFormat.size == null ? 12 : textFormat.size as int;
            _textFieldMeta.text = _target.text;
            _textFieldMeta.textColor = textFormat.color == null ? 0x000000 : textFormat.color as uint;
            _textFieldMeta.type = _target.type;
            _textFieldMeta.wordWrap = _target.wordWrap;
            _textFieldMeta.leading = Number(textFormat.leading);
            _textFieldMeta.indent = Number(textFormat.indent);
            _textFieldMeta.leftMargin = Number(textFormat.leftMargin);
            _textFieldMeta.rightMargin = Number(textFormat.rightMargin);
        }

        public static function fillRectangleMetaInfo(_target:CESRectangleMeta, _rect:Rectangle):void
        {
            _target.x = _rect.x;
            _target.y = _rect.y;
            _target.width = _rect.width;
            _target.height = _rect.height;
            _target.top = _rect.top;
            _target.bottom = _rect.bottom;
            _target.left = _rect.left;
            _target.right = _rect.right;
        }


    }
}
